package com.example.ythhome.login.api;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ythhome.busi.entity.BusiCode;
import com.example.ythhome.busi.service.BusiCodeService;
import com.example.ythhome.login.entity.*;
import com.example.ythhome.login.service.UserRoleService;
import com.example.ythhome.login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("api/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    BusiCodeService busiCodeService;

    @PostMapping("/query")
    @ResponseBody
    public ResponseData<IPage> query(@RequestBody RequestData<User> requestData){
        User user = requestData.getData();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_name", user.getUserName())
                .like("phone", user.getPhone())
                .orderByAsc("user_no");
        if(!"".equals(user.getStatus())&&user.getStatus()!=null){
            queryWrapper.eq("status", user.getStatus());
        }
        Page<User> page = new Page<>();
        page.setCurrent(requestData.getPagination().getCurrentPage());
        page.setSize(requestData.getPagination().getPageSize());
        return new ResponseData<IPage>(ResponseData.StatusCodeEnum_SUCCESS, userService.page(page,queryWrapper));
    };

    @PostMapping("/queryByUserNo")
    @ResponseBody
    public ResponseData<User> mine(@RequestBody String userNo){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_no", userNo);
        return new ResponseData<User>(ResponseData.StatusCodeEnum_SUCCESS, userService.getOne(queryWrapper));
    };


    @PostMapping("/add")
    @ResponseBody
    public ResponseData<Boolean> add(@RequestBody User user) {
        user.setStatus("1");
        user.setUserNo(busiCodeService.getBusiCode("USER_NO"));
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, userService.save(user));
    }

    @PostMapping("/edit")
    @ResponseBody
    public ResponseData<Boolean> edit(@RequestBody User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",user.getUserId());
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, userService.update(user,updateWrapper));
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseData<Boolean> delete(@RequestBody Integer id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, userService.remove(queryWrapper));
    }

    @PostMapping("/resetPwd")
    @ResponseBody
    public ResponseData<Boolean> resetPwd(@RequestBody User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id",user.getUserId());
        user.setUserPwd(SaSecureUtil.md5(user.getUserPwd()));
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, userService.update(user,updateWrapper));
    }

    @PostMapping("/doLogin")
    @ResponseBody
    public ResponseData<User> doLogin(@RequestBody LoginReq loginReq){
        if (userService.checkUserPassword(loginReq.getUsername(), SaSecureUtil.md5(loginReq.getPassword()))){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name",loginReq.getUsername());
            User user =userService.getOne(queryWrapper);
            StpUtil.login(user.getUserId());
            return new ResponseData<User>(ResponseData.StatusCodeEnum_SUCCESS,user );
        }else {
            return new ResponseData<User>(ResponseData.StatusCodeEnum_ERROR, new User());
        }
    };

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @GetMapping("/isLogin")
    @ResponseBody
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
    // 当前会话注销登录
    @GetMapping("/logout")
    @ResponseBody
    public String logout() {
        StpUtil.logout();
        return "当前会话是否登录：" ;
    }
}
